<?php
/*------------------------------------------------------------------------------ 
    Create Date :   25/10/2010
    Author    :      Nguyen Hoang Phuc
    Copyright(c) 2010 A.D.A Solution. All rights reserved
     
      
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#             Date          Author       Note
    Lephan  TRB1           2010/01/20      HoangPhuc    Xử lý xem bản đồ trường hợp Phương tiện có check tổng hợp
------------------------------------------------------------------------------*/
if (!defined('IN_ADAGPS')){
    die(SYSTEM_ACCESS_DENIED);
} 

global $objTemplate, $objDbSelect,$arrConfig;   

// dang ky su dung ham xajax 
$objXajax->registerFunction('ajxSaveData');
$objXajax->registerFunction('ajxStopData');
$objXajax->registerFunction('ajxAddService');
$objXajax->registerFunction('ajxLoadData');
$objXajax->registerFunction('ajxLoadMenu');
$objXajax->registerFunction('ajxLoadService');
$objXajax->registerFunction('ajxSaveCustomer'); 
$objXajax->registerFunction('ajxLoadCustomer');
$objXajax->registerFunction('ajxProcessService');   
$objXajax->registerFunction('ajxUpdateData');   
$objXajax->registerFunction('SaveMoneyTIP'); 
$objXajax->registerFunction('ajxLoadEmployee');     
$arrdetail = array();
$STT = 1; 
$displayOrder = 0;
$displayTitle = 1; 
function ajxLoadEmployee()
{
    global $objDbSelect;
     try
   {
       $xjax_respone= new xajaxResponse();
    // load danh sach nhân viên trực
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_current_employee c INNER JOIN tbl_employee e ON c.id_employee = e.id WHERE c.status = 1 AND c.is_service != 1 ";   
       $rs = $objDbSelect->Execute($sql);
       $sScript = ''; 
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id = $rs->fields['id']; 
                $url_image = $rs->fields['url_image'];
                $username = $rs->fields['username']; 
                $sScript .='<div  id="'.$id.'" class="gallery" style="width:64px;height:48px;display: inline-block;padding-right: 10px;margin-top: 0px;">';
                $sScript .= ' <img src="'.$url_image.'" id="'.$id.'" style="width:64px;height:48px"/>';
                $sScript .= '<h5 class="ui-widget-header" style="width:63x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
                $sScript .= '</div>';   
           }                                           
           while($rs->MoveNext());    
       }     
        $xjax_respone->addAssign('gallery', 'innerHTML', $sScript);
        $xjax_respone->addScript('$(document).ready(function(){
          newDrag_Drop.CreateDragDropGallery(); 
        });');
     return $xjax_respone -> getXML(); 
   }                      
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }                        
}
/*
* load tất cả các phòng của KS của khách sạn
*/
function ajxLoadData()
{
   global $objDbSelect,$arrdetail;
   try
   {
       $now = date('Y-m-d H:i:s'); 
       $sql = "SELECT r.id,CONCAT('room_',r.id) as name,if(rt.color is null,'#F0F0F0',rt.color) as color,r.room_no,c.timebegin,if(c.timebegin = '0000-00-00 00:00:00' or c.timebegin is null,0,UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(c.timebegin)) as duration,
       CONCAT('draggable','room_',r.id) as draggable,CONCAT('start','room_',r.id) as startclock,CONCAT('stop','room_',r.id) as stopclock, 
       CONCAT('customer','room_',r.id) as customerclock, CONCAT('hd','room_',r.id) as secondbegin,CONCAT('cl','room_',r.id) as clearclock ,
       CONCAT('cart','room_',r.id) as cart,CONCAT('label','room_',r.id) as label,
       if(c.timebegin = '0000-00-00 00:00:00' or c.timebegin is null,'',DATE_FORMAT(c.timebegin,': %Hh%i %d/%m/%y')) as displaytime,
       if (a.id_product is null ,0,a.id_product) as id_product,p.product_name,a.amount,if(c.money_tip is null ,0,c.money_tip) as money_tip,
       e.id as id_image,e.username,e.url_image,if(c.id_employee is null or c.id_employee = 0,0,1) as displayimage 
       FROM tbl_room r LEFT JOIN tbl_current_book_room c ON r.id = c.room_id LEFT JOIN tbl_employee e ON e.id = c.id_employee
       LEFT JOIN tbl_room_type rt ON r.room_type = rt.id  
       LEFT JOIN tbl_addservice a ON a.id_generate = c.id LEFT JOIN tbl_product p ON a.id_product = p.id 
       order by r.room_no,p.id";      
       $xjax_respone = new xajaxResponse();
       $oTbs = new clsTinyButStrong();   
       $oTbs ->LoadTemplate("../templates/vi/hotel_cafe_manager_grid.html") ;
       $rs = $objDbSelect->Execute($sql);
       $arr = RecordsetToArray($rs,$arrdetail);     
       $oTbs->MergeBlock('lstReport',$arr);
       $oTbs->MergeBlock('blk_list1', 'array', 'arrdetail[%p1%]');
       $oTbs->Show(TBS_NOTHING);
       $xjax_respone->addAssign('dvContent', 'innerHTML', $oTbs->Source);
       
       // load menu
       $sqlP = "SELECT t.id,t.name,p.product_name,p.id as id_product FROM tbl_product_type  t  LEFT JOIN tbl_product p ON t.id = p.product_type where t.parent_menu  = 0 order by p.product_type,p.product_name"; 
       $rs = $objDbSelect->Execute($sqlP);
       $scriptsubmenu = "var divTapArr =  new Array();";
       $product_type = ''; 
       $product_id = '';         
        if($rs->RecordCount() > 0)
           {  
               $i=-1;
               do
               {
                     $name = $rs->fields['name'];
                     $product_name = isset($rs->fields['product_name']) ? $rs->fields['product_name'] : '';
                     $id_product = $rs->fields['id_product'];
                     $product_type_id  =  $rs->fields['id'];
                     
                    if ($product_type != $rs->fields['id'])
                    {
                      $i++; 
                      $j=0;    
                      $scriptsubmenu.="divTapArr[".$i."] = new Array();";
                     
                    }
                    // tìm menu con của tab nay
                      $arrSub = array();
                      if ($product_name == '')
                      {
                       $subTab_sql = "SELECT id,name,parent_menu FROM tbl_product_type WHERE parent_menu = $product_type_id";
                       $arrSub = $objDbSelect->GetArray($subTab_sql);
                      }
                      $scriptsubmenu.="divTapArr[".$i."][".$j."] = new Array();";  
                      $scriptsubmenu.="divTapArr[".$i."][".$j."][0] = 'tab-".$product_type_id."';";
                      $scriptsubmenu.="divTapArr[".$i."][".$j."][1] = '".$name."';";      
                      $scriptsubmenu.="divTapArr[".$i."][".$j."][2] = '".$id_product."';";
                      $scriptsubmenu.="divTapArr[".$i."][".$j."][3] = '".$product_name."';";
                      if ($product_name == '' && count($arrSub) > 0)
                      {
                       $scriptsubmenu.="divTapArr[".$i."][".$j."][4] = new Array();";
                          for ($m = 0;$m<count($arrSub);$m++)
                          {
                             $name_sub = $arrSub[$m]['name']; 
                             $id_tap_sub = $arrSub[$m]['id'];
                             $submenu_sql = "SELECT t.id,t.name,p.product_name,p.id as id_product FROM tbl_product_type  t  LEFT JOIN tbl_product p ON t.id = p.product_type where t.id  = $id_tap_sub order by p.product_type,p.product_name";               
                             $arrSub_Menu = $objDbSelect->GetArray($submenu_sql);
                             $scriptsubmenu.="divTapArr[".$i."][".$j."][4][$m] = new Array();";  
                             $scriptsubmenu.="divTapArr[".$i."][".$j."][4][$m][0] = 'sub_tap-".$id_tap_sub."';"; 
                             $scriptsubmenu.="divTapArr[".$i."][".$j."][4][$m][1] = '$name_sub';"; 
                             $scriptsubmenu.="divTapArr[".$i."][".$j."][4][$m][2] = new Array();"; 
                             if (count($arrSub_Menu) > 0)
                             {
                                 for ($n = 0;$n<count($arrSub_Menu);$n++)
                                 {
                                   $p_name = $arrSub_Menu[$n]['product_name'];
                                   $id_name = $arrSub_Menu[$n]['id_product'];  
                                   $scriptsubmenu.="divTapArr[".$i."][".$j."][4][$m][2][$n] = '$id_name;$p_name';";                             
                                 }
                             }
                             
                             
                          }                    
                      }
                      $j++;
                    $product_type = $rs->fields['id'];                                    
               }                                           
               while($rs->MoveNext());
           
           }      
       $xjax_respone->addScript(' $(document).ready(function(){
                     '.$scriptsubmenu.'
                     newDrag_Drop.CreateDivTapChild(divTapArr); 
                   });');           
        
      $sqlP = "SELECT id,name,parent_menu FROM tbl_product_type order by id";
      $rs = $objDbSelect->Execute($sqlP);
      if($rs->RecordCount() > 0)
           {  
               $product_type = -1;
               do
               {
                if ($product_type != $rs->fields['id'])
                {
                 $name = $rs->fields['name']; 
                 $product_type = $rs->fields['id'];
                 $xjax_respone->addScript('
                   $(document).ready(function(){       
                     newDrag_Drop.CreateNewTap_Drag("#tab-'.$product_type.' li");
                    }); ');
                }
               }                                           
               while($rs->MoveNext());
           }            
     
       // load tat ca các phong
       $rs = $objDbSelect->Execute($sql);
       $id = 0;
       $js =""; 
       if($rs->RecordCount() > 0)
       {
           do
           {
               if($id != $rs->fields['id'])
               {
                $draggable_ID = $rs->fields['draggable'];
                $id = $rs->fields['id'];
                $name = $rs->fields['name']; 
                $startclock = $rs->fields['startclock'];
                $customerclock = $rs->fields['customerclock']; 
                $timebegin = $rs->fields['timebegin'];
                $displaytime = $rs->fields['displaytime'];
                $arrdetail_ProductID = $arrdetail[$id][0]['id_product'];
                $xjax_respone->addScript('
                   $(document).ready(function(){
                        var arrDataRoom = new Array();
                        arrDataRoom["timebegin"]= "'.$timebegin.'";
                        arrDataRoom["startclock"]= "#'.$startclock.'";
                        arrDataRoom["customerclock"]= "#'.$customerclock.'";    
                        arrDataRoom["name"]= "'.$name.'";
                        arrDataRoom["arrdetail_ProductID"]= "'.$arrdetail_ProductID.'"; 
                        arrDataRoom["btnOrderID"]= "#btnOrder'.$id.'";
                        arrDataRoom["imageEdit"]= "#imageEdit'.$id.'";        
                        newDrag_Drop.CreateNewDraggableObj("#'.$draggable_ID.'",arrDataRoom);
                        newDrag_Drop.CreateNewMenu("#cart'.$name.'");
                        newDrag_Drop.CreateDragDropEmployee($( "#trash'.$id.'" ));  
                    }); ');
               }   
           }                                           
           while($rs->MoveNext());    
       } 

       // load danh sach nhân viên trực
       $sql = "SELECT e.id,e.username,e.url_image FROM tbl_current_employee c INNER JOIN tbl_employee e ON c.id_employee = e.id WHERE c.status = 1 AND c.is_service != 1 ";   
       $rs = $objDbSelect->Execute($sql);
       $sScript = ''; 
       if($rs->RecordCount() > 0)
       {
           do
           {
                $id = $rs->fields['id']; 
                $url_image = $rs->fields['url_image'];
                $username = $rs->fields['username']; 
                $sScript .='<div  id="'.$id.'" class="gallery" style="width:64px;height:48px;display: inline-block;padding-right: 10px;margin-top: 0px;">';
                $sScript .= ' <img src="'.$url_image.'" id="'.$id.'" style="width:64px;height:48px"/>';
                $sScript .= '<h5 class="ui-widget-header" style="width:63x;margin-top:0px;text-align: center;">'.$username.'</h5>';     
                $sScript .= '</div>';   
           }                                           
           while($rs->MoveNext());    
       }     
        $xjax_respone->addAssign('gallery', 'innerHTML', $sScript);
       
        $xjax_respone->addScript('$(document).ready(function(){
          newDrag_Drop.CreateDragDropGallery(); 
        });'); 
       return $xjax_respone -> getXML(); 
   } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }                       
}
/*
* luu và cập nhật khi phòng dc thuê hoặc chuyển phòng
* param1 $id_room1 : phòng thuê hoặc phòng dc chuyển đến
* param2  $id_room2 : phòng được chuyển.
*/
function ajxSaveData($id_room1,$id_room2)
{
  global $objDbSelect;
  try
  {
      $now = date('Y-m-d H:i:s');
      // nếu nhấn start 
      if ($id_room2 == "")
      { 
       $sql1 = "INSERT INTO tbl_current_book_room (room_id,timebegin) VALUES($id_room1,'$now')";  
                
       $objDbSelect->Execute($sql1);
      }                                                                          
      // nếu chuyển từ máy 2 sang máy 1
      else
      {
       $sql1 = "UPDATE  tbl_current_book_room  SET room_id = $id_room1 WHERE room_id = $id_room2";    
       $objDbSelect->Execute($sql1);     
      } 
      $xjax_respone = new xajaxResponse();   
      return $xjax_respone -> getXML();
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }                   
}
/*
* luu và cập nhật thông tin của KH thuê phòng
* param1 $id_customer : id KH cần khi update KH
* param2  $id_room : phòng mà KH đang ở.
* param3 $arrParam : mảng chứa thông tin của KH
*/
function ajxSaveCustomer($id_customer,$id_room,$arrParam)
{
  global $objDbSelect;
  try
  {
      $xjax_respone = new xajaxResponse();
      $cmd = $arrParam['cmd'] ;
      $SQL ="";
          if ($cmd == 'save')
          {  
              $name = $arrParam['name'] ;
              $cmnd = $arrParam['cmnd'];
              $age = $arrParam['age'];
              $address =  $arrParam['address']; 
              if ($id_customer == '0' || $id_customer == '-1')
              {
                  $sqlS = "SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room'";
                  $rs = $objDbSelect->Execute($sqlS);
                  $id_generate = $rs->fields['id'];
                  $SQL = "INSERT INTO tbl_customer(id_generate,name,cmnd,age,address) VALUES($id_generate,'$name','$cmnd','$age','$address')";
              }
              else
              {
                  $SQL = "UPDATE  tbl_customer SET name = '$name',cmnd = '$cmnd',address = '$address',age = '$age' WHERE id = $id_customer";
              } 
          }
          else
          {
            $SQL = "DELETE FROM tbl_customer  WHERE id = $id_customer";
          }
          try {
                $objDbSelect->Execute($SQL);
          } 
          catch (Exception $e) {
              $xjax_respone->addAlert("Lỗi :".$e->getMessage());
           }     
          if ($cmd == 'delete' || ($cmd == 'save' && $id_customer == '0'))
          {
            $xjax_respone->addScript("xajax_ajxLoadCustomer($id_room);");  
          }
         
      return $xjax_respone -> getXML();
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }         
}
/*
* load ds KH đang thuê phòng khi nhấn button Thong tin KH 
* param1  $id_room : phòng mà KH đang ở.
*/
function ajxLoadCustomer($id_room) 
{              
  global $objDbSelect,$STT;
  try
  {
      $xjax_respone = new xajaxResponse();
      $sqlS =  "SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room'";
      $rs = $objDbSelect->Execute($sqlS);
      if (!$rs || $rs->RecordCount() == 0)
      {
           $xjax_respone->addScript("window.location.reload();"); 
            return $xjax_respone -> getXML();  
      }
      $SQL = "SELECT *  FROM tbl_customer WHERE id_generate in (SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room')";
      $rs = $objDbSelect->Execute($SQL);
      if ($rs->RecordCount() == 0)
      {
       $STT += 1;   
      }
      else
      {
       $STT += ($rs->RecordCount());   
      }
      $oTbs = new clsTinyButStrong();   
      $oTbs ->LoadTemplate("../templates/vi/hotel_cafe_customer_grid.html") ;
      $oTbs->Show(TBS_NOTHING); 
      $oTbs->MergeBlock('blk_data', $objDbSelect->_connectionID, $SQL);    
      $xjax_respone->addAssign('dialog-service', 'innerHTML', $oTbs->Source);   
      return $xjax_respone -> getXML();
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
}
/*
* xóa thông tin trong bảng tạm khi KH trả phòng và lưu vào lịch sử 
* param1  $id_room : phòng mà KH đang ở.
*/
function ajxStopData($id_room)
{
  global $objDbSelect;
  try
  {
      $now = date('Y-m-d H:i:s');
      $sqlS = "SELECT id,timebegin,id_employee,money_tip FROM tbl_current_book_room WHERE room_id = '$id_room'";
      $rs = $objDbSelect->Execute($sqlS);
      $time =  $rs->fields['timebegin'];
      $id_generate = $rs->fields['id'];
      $id_employee = $rs->fields['id_employee'];
      $money_tip = $rs->fields['money_tip'];    
      $sql1 = "INSERT INTO tbl_history_book_room (room_id,active,begin_time,end_time,date_created,id_generate,id_employee,money_tip) VALUES($id_room,1,'$time','$now','$now','$id_generate',$id_employee,$money_tip)";
      $sql2 = "DELETE FROM tbl_current_book_room  WHERE room_id = '$id_room'";
      $sql3 = "UPDATE tbl_current_employee SET is_service = 0 WHERE id_employee = '$id_employee'";
      $objDbSelect->Execute($sql1);
      $objDbSelect->Execute($sql2); 
      $objDbSelect->Execute($sql3);     
      $xjax_respone = new xajaxResponse();
      $xjax_respone->addScript('xajax_ajxLoadEmployee();');        
      return $xjax_respone -> getXML();
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }                   
}
/*
* Thêm vào db dich vụ khi  KH gọi. 
* param1  $id_room : phòng mà KH đang ở.
* param2  $id_product : mã sản phẩm KH gọi.
* param3  $soL mặc định = 1; khi KH cập nhật có thể lớn hơn 1
*/
function ajxAddService($id_room,$id_product,$soL=1)
{
  global $objDbSelect;
  try
  {
      $xjax_respone = new xajaxResponse();
      // lấy id_generate
      $sqlS = "SELECT id,timebegin FROM tbl_current_book_room WHERE room_id = '$id_room'";
      $rs = $objDbSelect->Execute($sqlS);
      $id_generate = $rs->fields['id'];
      
      $sqlS = "SELECT if(amount is null ,0,amount) as amount FROM tbl_addservice WHERE id_product = '$id_product' AND id_generate = '$id_generate'";
      $rs = $objDbSelect->Execute($sqlS);
      $amount = isset($rs->fields['amount']) ? $rs->fields['amount']  : 0;
      $soL +=$amount;
      if ($amount > 0)
      {
       $SQL = "UPDATE tbl_addservice SET amount = '$soL' WHERE id_product = '$id_product' AND id_generate = '$id_generate'"; 
      }
      else
      {
      $SQL = "INSERT INTO tbl_addservice(id_generate,id_product,date_created,amount) VALUES($id_generate,$id_product,now(),$soL)";    
      }
      $objDbSelect->Execute($SQL);
      $xjax_respone->addScript("xajax_ajxLoadService('$id_room')");
      return $xjax_respone -> getXML();                
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
}
/*
* cập nhật và xóa thông tin dịch vụ mà KH ko cần  
* param1  $id_room : phòng mà KH đang ở.
* param2  $id_generate : dùng để truy xuất bảng addservice.
* param3  $id_product : mã sản phẩm dùng để truy xuất bảng addservice.
* param4  cmd : save hoặc delete.    
*/
function ajxProcessService($id_room,$id_generate,$id_product,$amount,$cmd)
{
  global $objDbSelect;
  $xjax_respone = new xajaxResponse();
  try
  {
      if ($cmd == 'save')
      {
       $SQL = "UPDATE tbl_addservice SET amount = '$amount' WHERE id_product = '$id_product' AND id_generate = '$id_generate'"; 
       $objDbSelect->Execute($SQL);
       $xjax_respone->addScript("xajax_ajxLoadService('$id_room')");
      }
      else
      {
      $SQL = "DELETE FROM tbl_addservice WHERE id_product = '$id_product' AND id_generate = '$id_generate'";     
      $objDbSelect->Execute($SQL);
      $xjax_respone->addScript("xajax_ajxLoadService('$id_room',3)");
      }
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
  return $xjax_respone -> getXML();                
}
/*
* Load lại thông tin các dịch vụ mà mỗi lần KH cập nhật 
* param1  $id_room : phòng mà KH đang ở.
* param2  $viewOrder : có giá trị 1(load khi thêm mới),2(load khi tính tien),3(load lại khi cập nhật)   ,4 (load lại khi xóa dịch vụ)   
*/
function ajxLoadService($id_room,$viewOrder=1)
{
  global $objDbSelect,$displayOrder,$displayTitle;
  try{
      $displayOrder = $viewOrder;
      $xjax_respone = new xajaxResponse();
      $sqlS =  "SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room'";
      $rs = $objDbSelect->Execute($sqlS);
      if (!$rs || $rs->RecordCount() == 0)
      {
           $xjax_respone->addScript("window.location.reload();"); 
            return $xjax_respone -> getXML();  
      }
      $sqlS = "SELECT c.id,c.timebegin,m.price FROM tbl_current_book_room c LEFT JOIN tbl_room r ON c.room_id = r.id
               LEFT JOIN tbl_money_per_time_for_rent m ON m.room_type = r.room_type
               WHERE r.id = $id_room AND ((UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(c.timebegin))/3600 >  m.time_from) AND ((UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(c.timebegin))/3600  <= m.time_to)";
      $rs = $objDbSelect->Execute($sqlS);
      $moneyroom = isset($rs->fields['price']) ? $rs->fields['price'] : 0;
      $arrData1 = array();
      $arrData2 = array(); 
      $arrData1[]['moneyroom'] = $moneyroom;
      $limit = "";
      $SQL = "SELECT p.id,a.id_generate,a.amount as total,a.id_product,p.product_name,p.price,p.unit,p.price*a.amount as tt FROM tbl_addservice a INNER JOIN tbl_product p ON a.id_product = p.id 
          WHERE a.id_generate in (SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room') group by p.id order by p.product_name $limit";
      $SQL2 = "SELECT sum(p.price*a.amount) as tt, '$viewOrder' as cmd  FROM tbl_addservice a LEFT JOIN tbl_product p ON a.id_product = p.id 
      WHERE a.id_generate in (SELECT id FROM tbl_current_book_room WHERE room_id = '$id_room') group by a.id_generate";
      $rs = $objDbSelect->Execute($SQL2);
      $moneyService = isset($rs->fields['tt']) ? $rs->fields['tt'] : 0; 
      $moneytotal = $moneyService + $moneyroom; 
      $arrData2[]['moneytotal']= $moneytotal;
      if ($moneyService == 0)
      {
          $displayTitle = 0;
      }
      if ($viewOrder == 1 || $viewOrder == 3)
      {
           $xjax_respone->addScript('$( "#btnOrder'.$id_room.'" ).button({ label: "Hóa đơn" });$( "#btnOrder'.$id_room.'" ).button("enable");');               
          $rs = $objDbSelect->Execute($SQL);
          $script = '<ol style="margin-top:0px">'; 
           if($rs->RecordCount() > 0)
           {  
               $i= 1;
               do
               {
                if ($i > 3 && $viewOrder == 1)
                {
                    $script .= "<li>...</li>";
                    break;
                }   
                $id_product = $rs->fields['id_product'];
                $product_name = $rs->fields['product_name'];
                $total = $rs->fields['total']; 
                $script .= "<li>$product_name ($total)</li>"; 
                $i++;    
               }                                           
               while($rs->MoveNext());
               
           }
           else
           {
            $script .= ' <li class="placeholder">Add your items here</li>';   
            $xjax_respone->addScript('$( "#btnOrder'.$id_room.'" ).button({ label: "Hóa đơn" });$( "#btnOrder'.$id_room.'" ).button("disable");');
            $xjax_respone->addScript('$( "#dialog-service" ).dialog( "close" );');                                    
           }
          $script .= '<ol/>'; 
          $xjax_respone->addAssign('cartroom_'.$id_room, 'innerHTML', $script); 
          $xjax_respone->addScript('newDrag_Drop.CreateNewMenu("#cartroom_'.$id_room.'");');
      }
      if ($viewOrder == 2 || $viewOrder == 3 || $viewOrder == 4)
      {
        $oTbs = new clsTinyButStrong();   
        $oTbs ->LoadTemplate("../templates/vi/hotel_cafe_orderinfo_grid.html") ;
        $oTbs->Show(TBS_NOTHING); 
        $oTbs->MergeBlock('blk_data', $objDbSelect->_connectionID, $SQL);
        $oTbs->MergeBlock('blk_data2',$objDbSelect->_connectionID, $SQL2);
        $oTbs->MergeBlock('blk_data3',$arrData1);
        $oTbs->MergeBlock('blk_data4',$arrData2);               
        $xjax_respone->addAssign('dialog-service', 'innerHTML', $oTbs->Source);     
      } 
      return $xjax_respone -> getXML();
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
 return $xjax_respone -> getXML();                 
}
// cap nhat nhan vien phuc vu ban
function ajxUpdateData($employee_now,$employee_to,$room_id,$cmd)
{
  global $objDbSelect;
   $xjax_respone = new xajaxResponse();
   if ($cmd == 2)
   {
      $sql1 = "UPDATE tbl_current_employee SET is_service = 0 WHERE id_employee = '$employee_now'";
      $sql2 = "UPDATE tbl_current_book_room SET id_employee = 0 WHERE room_id = '$room_id'";
      $objDbSelect->Execute($sql1);
      $objDbSelect->Execute($sql2);  
   }
   else if ($cmd == 1)
   {
      $sql2 = "UPDATE tbl_current_employee SET is_service = 1 WHERE id_employee = '$employee_to'";  
      $objDbSelect->Execute($sql2);
      $sql3 = "UPDATE tbl_current_employee SET is_service = 0 WHERE id_employee = '$employee_now'";
      $objDbSelect->Execute($sql3); 
      $sql4 = "UPDATE tbl_current_book_room SET id_employee = 0 WHERE id_employee = '$employee_to'"; 
      $objDbSelect->Execute($sql4);            
      $sql4 = "UPDATE tbl_current_book_room SET id_employee = $employee_to WHERE room_id = '$room_id'"; 
      $objDbSelect->Execute($sql4);
      if ($employee_now != "" && $employee_to != "")
      {       
      $xjax_respone->addScript('xajax_ajxLoadEmployee();');  
      }
   } 
  return $xjax_respone -> getXML();             
}
// lưu tiền bo cho nhân viên 
function SaveMoneyTIP ($room_id,$tip)
{
  global $objDbSelect;
  $xjax_respone = new xajaxResponse();
  try
  {
   
    $SQL = "UPDATE tbl_current_book_room SET money_tip = '$tip' WHERE room_id = '$room_id'";  
    $objDbSelect->Execute($SQL); 
  
  } 
  catch (Exception $e) {
         // $xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
  return $xjax_respone -> getXML();                  
}
// hàm xủ lý dữ liêu cho hàm load data   
function RecordsetToArray($rs,&$arrdetail)
{  
  try
     { 
        $arr = array();
        $id_table = "-1";
        $id_product ="-1";
        $total = 0;
        $i=0;
        if(!$rs) return array();
        if($rs->RecordCount() == 0) return array();        
        do
        {                        
           $id_table =$rs ->fields['id'];
          
           $arr[$id_table]= array('id'=>$rs ->fields['id'] ,
                                  'name'=>$rs ->fields['name'],
                                  'room_no'=>$rs ->fields['room_no'] ,
                                  'timebegin'=> $rs ->fields['timebegin'],
                                  'duration'=> $rs ->fields['duration'],
                                  'draggable'=> $rs ->fields['draggable'],
                                  'startclock'=> $rs ->fields['startclock'],
                                  'stopclock'=> $rs ->fields['stopclock'],
                                  'customerclock'=> $rs ->fields['customerclock'],
                                  'secondbegin'=> $rs ->fields['secondbegin'] ,
                                  'clearclock'=> $rs ->fields['clearclock'] ,           
                                  'cart'=> $rs ->fields['cart'],
                                  'color'=> $rs ->fields['color'],
                                  'label'=> $rs ->fields['label'], 
                                  'displaytime'=> $rs ->fields['displaytime'],
                                  'id_product' =>$rs ->fields['id_product'],
                                  'id_image' =>$rs ->fields['id_image'],
                                  'username' =>$rs ->fields['username'],
                                  'displayimage' =>$rs ->fields['displayimage'],
                                  'url_image' =>$rs ->fields['url_image'],
                                  'money_tip' =>$rs ->fields['money_tip']                                                                                                
                                );                                                 
           $amount = '';                     
           if (isset($rs ->fields['amount']))
           {
             $amount = '('.$rs ->fields['amount'].')';  
           }
           if (count ($arrdetail[$id_table]) <= 2 )
           {                                                            
           $arrdetail[$id_table][] = array('id_product' =>$rs ->fields['id_product'],
                                           'product_name' =>$rs ->fields['product_name'],
                                           'product_total' =>$amount);                           
                                           
           }
            else if (count ($arrdetail[$id_table]) == 3 )
           {
              $arrdetail[$id_table][] = array('id_product' =>$rs ->fields['id_product'],
                                           'product_name' =>'...',
                                           'product_total' =>'');  
           }
        }
        while($rs->MoveNext());  
        return $arr;
        } 
  catch (Exception $e) {
          //$xjax_respone->addAlert("Lỗi :".$e->getMessage());
  }      
  }
?>
